﻿using System;
using System.Collections.Generic;
using System.Text;


using CMN.Common;
using CMN.Entity;
using CMN.BLL;
using CMN.Cache;
using CMN.Extends;
using System.IO;
namespace CMN.Extends.Job
{
    public class JobCollcetList : IJob
    {
        /// <summary>
        /// 执行任务
        /// </summary>
        /// <param name="obj"></param>
        public void Execute(object obj)
        {
            //if (DateTime.Now.Hour > 10 || DateTime.Now.Hour < 22)
            //{
            //    System.Threading.Thread.Sleep(100000);
            //    return;
            //}
            try
            {

                QueryParam qp = new QueryParam();
                qp.ReturnFields = "*";
                qp.PageIndex = 1;
                qp.PageSize = int.MaxValue;
                qp.Where = "enabled=1 ";
                qp.Orderfld = "CollectTime asc";
                int recordNum = 0;
                Extend_CollectLinkInfo bllLinkInfo = new Extend_CollectLinkInfo();
                Extend_CollectSetInfo bllSetInfo = new Extend_CollectSetInfo();
                List<Extend_CollectSetInfoEntity> setList = CacheCollcetSet.CollectSetInfoList();
                foreach (Extend_CollectSetInfoEntity setInfo in setList)
                {
                    //AppLog.Write(setInfo.Name, AppLog.LogMessageType.Error);
                    if (setInfo.Enabled != true)
                    {
                        setInfo.CollectTime = DateTime.Now;
                        //break;
                    }
                    if (setInfo.CollectTime == null || setInfo.CollectTime.Value.AddMinutes(setInfo.DoTime) <= DateTime.Now)
                    {
                        try
                        {
                            CollectHelper chelper = new CollectHelper();
                            List<Extend_CollectLinkInfoEntity> linkList = chelper.GetLinks(setInfo);
                            foreach (Extend_CollectLinkInfoEntity linkInfo in linkList)
                            {
                                linkInfo.CollectSetId = setInfo.Id;
                                Extend_CollectLinkInfoEntity oldLinkInfo = bllLinkInfo.Disp(linkInfo.Url);
                                if (oldLinkInfo==null)
                                {
                                    linkInfo.DataTable_Action = DataTableAction.Insert;
                                    bllLinkInfo.InsertUpdateDelete(linkInfo);
                                }
                                else
                                {
                                    if (setInfo.OrderNum==100)
                                    {
                                        if (oldLinkInfo.CollectNum != 9)
                                        {
                                            oldLinkInfo.CollectNum = 9;
                                            oldLinkInfo.DataTable_Action = DataTableAction.Update;
                                            bllLinkInfo.InsertUpdateDelete(oldLinkInfo);
                                        }
                                    }
                                }
                            }
                            setInfo.CollectTime = DateTime.Now;
                            setInfo.DataTable_Action = DataTableAction.Update;
                            bllSetInfo.InsertUpdateDelete(setInfo);
                            //AppLog.Write(string.Concat("采集列表--", setInfo.Name, "--数量:", linkList.Count.ToString()), AppLog.LogMessageType.Debug);
                        }
                        catch (Exception ex)
                        {
                            AppLog.Write("采集列表出错" + setInfo.FirstUrl, AppLog.LogMessageType.Error, ex);
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                AppLog.Write("采集列表出错"+ex.Message, AppLog.LogMessageType.Error, ex);
            }
              
        }

        public void Error(Exception e)
        {
            AppLog.Write("采集列表出错", AppLog.LogMessageType.Error, e);
        }
    }
}
